﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Admin.Controls
{
    public partial class Category : System.Web.UI.UserControl
    {
        public int count = 1;
        public static int idCat = 0;
        DataTable dt;
        public static List<LIB.Sort> mysort = new List<LIB.Sort>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                mysort.Clear();
                Cate1.Items.Clear();
                dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereIDSort("tbl_Category", "Category_ID2", "0","Category_Ordinal","ASC");
                ListItem lt = new ListItem("Chọn danh mục", "-1");
                Cate1.Items.Add(lt);
                LIB.Sort s;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    s = new LIB.Sort(dt.Rows[i]["Category_ID1"].ToString(), dt.Rows[i]["Category_Ordinal"].ToString());
                    mysort.Add(s);
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    Cate1.Items.Add(lt);
                }
                Cate1.SelectedIndex = 0;
                rpt_Sanpham.DataSource = dt;
                rpt_Sanpham.DataBind();
                count = 1;
                idCat = 1;
            }
            lblIndex.Text = "Bạn đang xem danh sách danh mục gốc (danh mục cấp 1)";

        }
        protected void Cate1_SelectedIndexChanged(object sender, EventArgs e)
        {
            count = 1;
            Cate2.Items.Clear();
            mysort.Clear();
            int index = Cate1.SelectedIndex;
            if (index > 0)
            {
                int id = Convert.ToInt32(Cate1.SelectedValue);
                dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category", "Category_ID2", id.ToString());
                ListItem lt = new ListItem("Chọn danh mục", "-1");
                Cate2.Items.Add(lt);
                LIB.Sort s;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    s = new LIB.Sort(dt.Rows[i]["Category_ID1"].ToString(), dt.Rows[i]["Category_Ordinal"].ToString());
                    mysort.Add(s);
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_Ordinal"].ToString());
                    Cate2.Items.Add(lt);

                }
                lblIndex.Text = "Bạn đang xem danh sách danh mục cấp 2 của danh mục cấp 1";
                lblcate.Text = Cate1.SelectedItem.Text.ToString();
                rpt_Sanpham.DataSource = dt;
                rpt_Sanpham.DataBind();
            }
            idCat = 2;

        }

        protected void Cate2_SelectedIndexChanged(object sender, EventArgs e)
        {

            count = 1;
            mysort.Clear();
            int index = Cate2.SelectedIndex;
            if (index > 0)
            {
                LIB.Sort s;
                int id = Convert.ToInt32(Cate2.SelectedValue);
                dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category", "Category_ID2", id.ToString());
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    s = new LIB.Sort(dt.Rows[i]["Category_ID1"].ToString(), dt.Rows[i]["Category_Ordinal"].ToString());
                    mysort.Add(s);
                }
                lblIndex.Text = "Bạn đang xem danh sách danh mục cấp 3 của danh mục cấp 2";
                lblcate.Text = Cate2.SelectedItem.Text.ToString();
                rpt_Sanpham.DataSource = dt;
                rpt_Sanpham.DataBind();
            }
            idCat = 3;

        }

        protected void rpt_Sanpham_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "imgup")
            {
                int id = Convert.ToInt32(e.CommandArgument.ToString());

                int i = 0;
                for (i = 0; i < mysort.Count; i++)
                {
                    if (mysort[i].id == id.ToString())
                    {
                        break;
                    }
                }


                if (i > 0)
                {
                    DAL.DCollector.DBCollector.Instance.CallStored("callstored", "Update tbl_Category SET Category_Ordinal=" + mysort[i - 1].index + " Where Category_ID1=" + mysort[i].id + " AND Category_ID2 = 0", false);
                    DAL.DCollector.DBCollector.Instance.CallStored("callstored", "Update tbl_Category SET Category_Ordinal=" + mysort[i].index + " Where Category_ID1=" + mysort[i - 1].id + " AND Category_ID2 = 0", false);
                    Response.Redirect("/Admin/Index.aspx?panel=2&sub=1");
                }
            }

            if (e.CommandName == "imgdown")
            {
                int id = Convert.ToInt32(e.CommandArgument.ToString());

                int i = 0;
                for (i = 0; i < mysort.Count; i++)
                {
                    if (mysort[i].id == id.ToString())
                    {
                        break;
                    }
                }


                if ( i < mysort.Count - 1)
                {

                    DAL.DCollector.DBCollector.Instance.CallStored("callstored", "Update tbl_Category SET Category_Ordinal=" + mysort[i + 1].index + " Where Category_ID1=" + mysort[i].id + " AND Category_ID2 = 0", false);
                    DAL.DCollector.DBCollector.Instance.CallStored("callstored", "Update tbl_Category SET Category_Ordinal=" + mysort[i].index + " Where Category_ID1=" + mysort[i + 1].id + " AND Category_ID2 = 0", false);
                    Response.Redirect("/Admin/Index.aspx?panel=2&sub=1");
                }
            }

            if (e.CommandName == "delete")
            {
                int id = Convert.ToInt32(e.CommandArgument.ToString());
                if (idCat == 1)
                {

                    dt = DAL.DCollector.DBCollector.Instance.CallStored("callstored","select * from tbl_Product Where Category_ID" + idCat + " =" + id, true);

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        int idpro = Convert.ToInt32(dt.Rows[i]["Product_ID"].ToString());

                        File.Delete(MapPath(dt.Rows[i]["Product_Image"].ToString()));
                        // goi ham xoa sp 
                        DataTable dtProduct = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Product", "Product_ID", idpro.ToString());
                        string imageavatar = dtProduct.Rows[i]["Product_Image"].ToString();
                        File.Delete(MapPath(imageavatar));

                        string smallimage = dtProduct.Rows[i]["Product_MultiImage"].ToString();
                        string[] splitimagesmall = smallimage.Split(',');

                        File.Delete(MapPath(splitimagesmall[0]));
                        File.Delete(MapPath(splitimagesmall[1]));
                        File.Delete(MapPath(splitimagesmall[2]));


                       
                       
                        DAL.DCollector.DBCollector.Instance.StoredDelete("tbl_Product", idpro.ToString());
                    }

                    // Xoa vendor

                    dt = DAL.DCollector.DBCollector.Instance.CallStored( "callstored","select * from tbl_Vendor Where Category_ID1=" + id, true);

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        File.Delete(MapPath(dt.Rows[i]["Vendor_LoGo"].ToString()));
                    }
                    DAL.DCollector.DBCollector.Instance.CallStored( "callstored","delete from tbl_Vendor Where Category_ID1=" + id, false);

                    // Xoa het anh cate con

                    dt = DAL.DCollector.DBCollector.Instance.CallStored("callstored","select * from tbl_Category Where Category_ID2=" + id, true);

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        File.Delete(MapPath(dt.Rows[i]["Category_Image"].ToString()));
                    }


                    DAL.DCollector.DBCollector.Instance.CallStored("callstored","delete from tbl_Category Where Category_ID2=" + id, false);

                    DAL.DCollector.DBCollector.Instance.CallStored("callstored","delete from tbl_Category Where Category_ID1=" + id, false);
                }
                else
                {

                    dt = DAL.DCollector.DBCollector.Instance.CallStored("callstored","select * from tbl_Product Where Category_ID" + idCat + " =" + id, true);

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        int idpro = Convert.ToInt32(dt.Rows[i]["Product_ID"].ToString());

                        File.Delete(MapPath(dt.Rows[i]["Product_Image"].ToString()));
                        // goi ham xoa sp 
                        DataTable dtProduct = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Product", "Product_ID", idpro.ToString());
                        string imageavatar = dtProduct.Rows[i]["Product_Image"].ToString();
                        File.Delete(MapPath(imageavatar));

                        string smallimage = dtProduct.Rows[i]["Product_MultiImage"].ToString();
                        string[] splitimagesmall = smallimage.Split(',');

                        File.Delete(MapPath(splitimagesmall[0]));
                        File.Delete(MapPath(splitimagesmall[1]));
                        File.Delete(MapPath(splitimagesmall[2]));




                        DAL.DCollector.DBCollector.Instance.StoredDelete("tbl_Product",idpro.ToString());
                    }

                    // Xoa het anh cate con

                    dt = DAL.DCollector.DBCollector.Instance.CallStored("callstored","select * from tbl_Category Where Category_ID2=" + id, true);

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        File.Delete(MapPath(dt.Rows[i]["Category_Image"].ToString()));
                    }


                    DAL.DCollector.DBCollector.Instance.CallStored("callstored","delete from tbl_Category Where Category_ID2=" + id, false);

                    DAL.DCollector.DBCollector.Instance.CallStored("callstored","delete from tbl_Category Where Category_ID1=" + id, false);
                }
                Response.Redirect("/Admin/Index.aspx?panel=2&sub=1");
            }

        }

        protected void lbtRoot_Click(object sender, EventArgs e)
        {
            Cate1.Items.Clear();
            Cate2.Items.Clear();
            dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category", "Category_ID2", "0");
            ListItem lt = new ListItem("Chon danh muc", "-1");
            Cate1.Items.Add(lt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                Cate1.Items.Add(lt);
            }
            Cate1.SelectedIndex = 0;
            rpt_Sanpham.DataSource = dt;
            rpt_Sanpham.DataBind();
            count = 1;
            idCat = 1;
            lblIndex.Text = "Bạn đang xem danh sách danh mục gốc (danh mục cấp 1)";
            lblcate.Text = "";
        }
    }
}